package com.dsz.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.dsz.po.House;
import com.dsz.po.User;
import com.dsz.service.UserService;

/**
 * 用户Controller类
 * 
 * @author 吴生伟
 *
 */
@Controller
public class UserController {
	// 依赖注入
	@Autowired
	private UserService userService;

	// 跳转到登录页
	@RequestMapping(value = "tologin.action")
	public String tologin() {
		return "/user/sign-in";

	}

	// 登录
	@RequestMapping(value = "login.action", method = RequestMethod.POST)
	public String login(String username, String password, Model model, HttpSession session) {
		// 通过账号密码查询用户
		User user = userService.findUser(username, password);
		if (user != null) {
			// 将对象用户添加到Session
			session.setAttribute("USER_SESSION", user);
			//测试查看
			System.out.println("登录成功，账号为："+user.getUsername());
			// 跳转到主页面
			return "redirect:index.jsp";
		}
		model.addAttribute("msg", "账号或密码错误，请重新输入!");
		return "/user/sign-in";

	}

	// 跳转到注册
	@RequestMapping(value = "toRegister.action")
	public String toRegiater() {
		return "user/sign-up";
	}

	// 注册
	@RequestMapping(value = "Register.action",method = RequestMethod.POST)
	public String Register(User user) {
		int rows = userService.addUser(user);
		if(rows>0) {
			System.out.println("注册成功");
			return "redirect:index.jsp";
		}else {
			System.out.println("注册失败");
			return "user/sign-up";
			
		}
		
	}
	
	//跳转到总览页面
	@RequestMapping(value="gallery.action")
	public String togallery() {
		return "user/gallery";
		
	}
	
	
	//跳转到个人中心页面
	@RequestMapping(value="person.action")
	public String toperson() {
		return "user/person";
	}
}
